package com.lxr0860.lxr0860production_test.view;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatDialog;
import androidx.core.content.ContextCompat;

import com.lxr0860.lxr0860production_test.R;

/**
 * 类描述:[通用多选对话框]
 *
 * @author: LXR-zsm
 * @Date: 2023/8/23 13:57
 */
public class CommonDialog extends AppCompatDialog {
    private Button btConfirm, btnClose;
    private TextView tvTitle, tvContent;
    private Activity activity;
    private BtnClickListener btnClickListener;
    private String title, msg,leftTxt,rightTxt;

    public CommonDialog(Activity activity, String title, String msg, BtnClickListener btnClickListener) {
        super(activity);
        this.activity = activity;
        this.title = title;
        this.msg = msg;
        this.btnClickListener = btnClickListener;
    }

    public CommonDialog(Activity activity, String title, String msg, String leftTxt, String rightTxt, BtnClickListener btnClickListener) {
        super(activity);
        this.activity = activity;
        this.title = title;
        this.msg = msg;
        this.leftTxt = leftTxt;
        this.rightTxt=rightTxt;
        this.btnClickListener = btnClickListener;
    }

    boolean resultNormal = true;

    public CommonDialog(Activity activity, String title, String msg, boolean resultNormal, BtnClickListener btnClickListener) {
        super(activity);
        this.activity = activity;
        this.title = title;
        this.msg = msg;
        this.btnClickListener = btnClickListener;
        this.resultNormal = resultNormal;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        setContentView(R.layout.dialog_common);
        initWindow();
        tvTitle = findViewById(R.id.dialog_title);
        tvContent = findViewById(R.id.dialog_content);
        btConfirm = findViewById(R.id.positiveButton);
        btnClose = findViewById(R.id.negativeButton);
        if (leftTxt!=null)
            btnClose.setText(leftTxt);
        if (rightTxt!=null)
            btConfirm.setText(rightTxt);
        if (!resultNormal) {
            btnClose.setText("重新测试");
            tvContent.setText("结果存在异常，是否继续保存？");
            tvContent.setTextColor(ContextCompat.getColor(activity,R.color.red_overlay));
        }

        if (title != null) {
            tvTitle.setText(title);
        }
        if (msg != null) {
            tvContent.setText(msg);
        }
        btConfirm.setOnClickListener(view -> {
            btnClickListener.onPositiveClickListener();
            this.cancel();
        });

        btnClose.setOnClickListener(view -> {
            btnClickListener.onNegativeClickListener(resultNormal);
            this.cancel();
        });

    }

    @Override
    public boolean onKeyDown(int keyCode, @NonNull KeyEvent event) {
        return true;
    }

    private void initWindow() {
        Window window = getWindow();
        if (null != window) {
            window.setGravity(Gravity.CENTER);
            WindowManager.LayoutParams windowParams = window.getAttributes();
            DisplayMetrics dm = new DisplayMetrics();
            activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
            window.setLayout(dm.widthPixels, window.getAttributes().height);
            windowParams.width = dm.widthPixels / 10 * 8;
            windowParams.dimAmount = 0.8f;
            window.setAttributes(windowParams);
        }
        setCanceledOnTouchOutside(false);
    }


    @Override
    public void show() {
        super.show();
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }


    public interface BtnClickListener {
        void onPositiveClickListener();

        void onNegativeClickListener(boolean isFinish);
    }
}
